define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var detectFields = [
        '厚度(左)', '厚度(中)', '厚度(右)', '密度', '回弹', '表面处理', '宽度', '米数', '硬度', '应力要求CFD (25%)', '外观', '标签打印选择模式'
    ];

    function renderTable(data) {
        var tbody = $('#detect-data-table tbody');
        tbody.empty();
        if (!data || !Array.isArray(data) || data.length === 0) {
            data = [{}];
        }
        data.forEach(function(row, idx) {
            var tr = $('<tr></tr>');
            detectFields.forEach(function(field) {
                tr.append('<td><input type="text" class="form-control input-sm" value="' + (row[field] || '') + '" data-field="' + field + '"></td>');
            });
            tr.append('<td><button type="button" class="btn btn-danger btn-xs btn-del-row">删除</button></td>');
            tbody.append(tr);
        });
    }

    function getTableData() {
        var data = [];
        $('#detect-data-table tbody tr').each(function() {
            var row = {};
            $(this).find('input[data-field]').each(function() {
                row[$(this).data('field')] = $(this).val();
            });
            data.push(row);
        });
        return data;
    }

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking_testing_new/index' + location.search,
                    add_url: 'clothmaking_testing_new/add',
                    edit_url: 'clothmaking_testing_new/edit',
                    del_url: 'clothmaking_testing_new/del',
                    multi_url: 'clothmaking_testing_new/multi',
                    import_url: 'clothmaking_testing_new/import',
                    table: 'clothmaking_testing_new',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'plan_id', title: __('Plan_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'detect_data', title: __('Detect_data'),
                            cellStyle: function(){return {css: {"max-width": "200px", "white-space": "nowrap", "overflow": "hidden", "text-overflow": "ellipsis"}};}
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            // 绑定编辑按钮
            $('#btn-edit-detect-data').on('click', function() {
                var val = $('#c-detect_data').val();
                var data = [];
                try {
                    data = val ? JSON.parse(val) : [];
                } catch (e) {
                    data = [{}];
                }
                renderTable(data);
                $('#detectDataModal').modal('show');
            });
            // 新增行
            $('#add-detect-row').on('click', function() {
                var data = getTableData();
                data.push({});
                renderTable(data);
            });
            // 删除行
            $('#detect-data-table').on('click', '.btn-del-row', function() {
                $(this).closest('tr').remove();
            });
            // 保存
            $('#save-detect-data').on('click', function() {
                var data = getTableData();
                $('#c-detect_data').val(JSON.stringify(data));
                $('#detectDataModal').modal('hide');
            });
        },
        edit: function () {
            Controller.getTableData($('#c-plan_id').val());
            Controller.api.bindevent();
            // 绑定编辑按钮
            $('#btn-edit-detect-data').on('click', function() {
                var val = $('#c-detect_data').val();
                var data = [];
                try {
                    data = val ? JSON.parse(val) : [];
                } catch (e) {
                    data = [{}];
                }
                renderTable(data);
                $('#detectDataModal').modal('show');
            });
            $('#add-detect-row').on('click', function() {
                var data = getTableData();
                data.push({});
                renderTable(data);
            });
            $('#detect-data-table').on('click', '.btn-del-row', function() {
                $(this).closest('tr').remove();
            });
            $('#save-detect-data').on('click', function() {
                var data = getTableData();
                $('#c-detect_data').val(JSON.stringify(data));
                $('#detectDataModal').modal('hide');
            });
        },
        api: {
            bindevent: function () {
                $("#c-plan_id").data("eSelect", function(data){
                    //后续操作
                    debugger
                    Controller.getTableData(data.id);
                });
                Form.api.bindevent($("form[role=form]"));
            }
        },
        getTableData: function(plan_id) {
            $.ajax({
                url: 'clothmaking_testing_new/getStandardInfoByPlanId',
                data: {plan_id: plan_id},
                success: function (res) {
                    thickness = res.thickness1 * res.thickness1_standard;
                    density = res.density1 * res.density1_standard;
                    spring_back = res.spring_back;
                    surface_treatment = res.surface_treatment;
                    width = res.width;
                    length = res.length;
                    // $("#detect-data-table").bootstrapTable('load', res);
                    $('.thickness_left').text('厚度(左):' + thickness);
                    $('.thickness_center').text('厚度(中):' + thickness);
                    $('.thickness_right').text('厚度(右):' + thickness);
                    $('.density').text('密度:' + density);
                    $('.spring_back').text('回弹:' + spring_back);
                    $('.surface_treatment').text('表面处理:' + surface_treatment);
                    $('.width').text('宽度:' + width);
                    $('.length').text('米数:' + length);
                    
                    debugger
                }
            }); 
        }
    };
    return Controller;
});
